package com.vr.cloudpicture.manager.auth;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.JSONUtil;
import com.vr.cloudpicture.enums.SpaceRoleEnum;
import com.vr.cloudpicture.enums.SpaceTypeEnum;
import com.vr.cloudpicture.manager.auth.model.SpaceUserAuthConfig;
import com.vr.cloudpicture.manager.auth.model.SpaceUserRole;
import com.vr.cloudpicture.model.dto.user.UserLogin;
import com.vr.cloudpicture.model.entity.Space;
import com.vr.cloudpicture.model.entity.SpaceUser;
import com.vr.cloudpicture.service.ISpaceUserService;
import com.vr.cloudpicture.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * 空间用户认证
 *
 * @author hzh
 * @date 2025/05/27
 */
@Component
@RequiredArgsConstructor
public class SpaceUserAuthManager {
    private final ISpaceUserService spaceUserService;
    private final IUserService userService;
    private static final SpaceUserAuthConfig SPACE_USER_AUTH_CONFIG;

    // 静态初始化
    static {
        String json = ResourceUtil.readUtf8Str("biz/spaceUseAuthConfig.json");
        SPACE_USER_AUTH_CONFIG = JSONUtil.toBean(json, SpaceUserAuthConfig.class);
    }

    /**
     * 通过角色key获取权限
     *
     * @param spaceUserRole 空间用户角色
     * @return {@code List<String> }
     */
    public List<String> getPermissionsByRole(String spaceUserRole) {
        if (spaceUserRole == null) {
            return Collections.emptyList();
        }
        // 找到匹配的角色
        SpaceUserRole role = SPACE_USER_AUTH_CONFIG.getRoles().stream()
                .filter(r -> r.getKey().equals(spaceUserRole))
                .findFirst()
                // 不存在就返回null
                .orElse(null);
        if (role == null) {
            return Collections.emptyList();
        }
        return role.getPermissions();
    }

    public List<String> getPermissionList(Space space, UserLogin userLogin) {
        if (userLogin == null) {
            // 未登录，没权限
            return Collections.emptyList();
        }
        List<String> adminPermissions = getPermissionsByRole(SpaceRoleEnum.ADMIN.getValue());
        if (space == null) {
            // 公共图库
            return adminPermissions;
        }
        // 私有空间或者团队空间
        SpaceTypeEnum spaceTypeEnum = SpaceTypeEnum.getEnumByValue(space.getSpaceType());
        if (spaceTypeEnum == null) {
            return Collections.emptyList();
        }
        switch (spaceTypeEnum) {
            case PRIVATE:
                // 仅本人或者管理员有权限
                if (space.getUserId().equals(userLogin.getId()) || userService.isAdmin(userLogin)) {
                    return adminPermissions;
                }
                break;
            case TEAM:
                // 团队空间，通过spaceUser来获取权限
                SpaceUser spaceUser = spaceUserService.lambdaQuery()
                        .eq(SpaceUser::getUserId, userLogin.getId())
                        .eq(SpaceUser::getSpaceId, space.getId())
                        .one();
                if (spaceUser == null) {
                    return Collections.emptyList();
                }
                return getPermissionsByRole(spaceUser.getSpaceRole());
        }
        return Collections.emptyList();
    }
}
